//
//  FocusImageTableViewCell.m
//  喜马拉雅FM
//
//  Created by dllo on 16/3/3.
//  Copyright © 2016年 lanou.com. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height


#import "FocusImageTableViewCell.h"

#import "ModelForData.h"
#import "UIImageView+WebCache.h"

@interface FocusImageTableViewCell ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, retain) NSMutableArray *arrForScroll;
@property (nonatomic, retain) UIPageControl *page;
@property (nonatomic, retain) NSTimer *timer;

@end


@implementation FocusImageTableViewCell

- (void)dealloc {
    
    [_scroll release];
    [_arrForScroll release];
    [_page release];
    [_timer release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createScrollView];
        [self createPageView];
        
    }
    return self;
}

#pragma mark createScrollView
- (void)createScrollView {
    
    self.scroll = [[UIScrollView alloc]init];
    [self.contentView addSubview:self.scroll];
    self.scroll.frame = CGRectMake(0, 0, WIDTH, WIDTH * 3/5);
    self.scroll.contentOffset = CGPointMake(WIDTH, 0);
    self.scroll.pagingEnabled = YES;
    self.scroll.bounces = YES;
    self.scroll.delegate = self;
    [self.scroll release];
    
}

- (void)createPageView {
    
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(WIDTH / 7, WIDTH * 3/5 - 40, WIDTH / 2, 40)];
    [self.contentView addSubview:self.page];
    self.page.backgroundColor = [UIColor clearColor];
    self.page.currentPageIndicatorTintColor = [UIColor blackColor];
    self.page.pageIndicatorTintColor = [UIColor grayColor];
    [_page release];
    
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
}

- (void)pageAction:(UIPageControl *)page {
    
    
    /** 带动画效果. */
    //    [self.scroll setContentOffset:CGPointMake(page.currentPage * WIDTH, 0) animated:YES];
    
}

#pragma mark 赋值方法实现.
- (void)passModelForDataToCellOne:(ModelForData *)model {
    
    self.arrForScroll = [NSMutableArray array];
    if (model.arrForPic && self.arrForScroll) {
    
    NSMutableArray *arrTemp = [NSMutableArray arrayWithArray:model.arrForPic];
    
    for (NSDictionary *dic in arrTemp) {
        NSString *str = [dic objectForKey:@"pic"];
        [self.arrForScroll addObject:str];
    }
    if (self.arrForScroll.count != 0) {
        [self.arrForScroll addObject:[self.arrForScroll firstObject]];
        [self.arrForScroll insertObject:[self.arrForScroll objectAtIndex:(self.arrForScroll.count - 2)] atIndex:0];
        
        self.scroll.contentSize = CGSizeMake(WIDTH * self.arrForScroll.count, WIDTH * 3/5);
        // 页面数.
        self.page.numberOfPages = self.arrForScroll.count - 2;
    }
    for (int i = 0; i < self.arrForScroll.count; i++) {
        UIImageView *pic = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.jpg"]];
        [pic sd_setImageWithURL:[NSURL URLWithString:self.arrForScroll[i]]];
        pic.frame = CGRectMake(WIDTH * i, 0, WIDTH, WIDTH * 3/5);
        pic.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
        [pic addGestureRecognizer:tap];
        
        [self.scroll addSubview:pic];
        [pic release];
    }
    }
}

- (void)handleTap:(UITapGestureRecognizer *)tap {

    NSLog(@"轮播图");

}

#pragma mark ScrollView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (self.scroll.contentOffset.x == 0) {
        self.scroll.contentOffset = CGPointMake(WIDTH * (self.arrForScroll.count - 2), 0);
    }
    if (self.scroll.contentOffset.x == WIDTH * (self.arrForScroll.count - 1)) {
        self.scroll.contentOffset = CGPointMake(WIDTH, 0);
    }
    self.page.currentPage = scrollView.contentOffset.x / WIDTH - 1;
    
}





/** 自动轮播 **/
- (void)setUpTimer{
    
    self.timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timerChanger) userInfo:nil repeats:YES];
    
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
    self.timer = _timer;
    
    
}

//** 翻页控制 *
- (void)timerChanger {
    
    if (self.scroll.contentOffset.x == WIDTH * 7) {
        
        self.page.currentPage = 0;
        
        [UIView animateWithDuration:0.3 animations:^{
            
            int contentOfset = (int)self.scroll.contentOffset.x;
            self.scroll.contentOffset = CGPointMake(contentOfset + WIDTH, 0);
        }];
        
    }else {
        
        if (self.scroll.contentOffset.x == 0) {
            self.scroll.contentOffset = CGPointMake(WIDTH * 7, 0);
        }
        if (self.scroll.contentOffset.x == WIDTH * 8) {
            self.scroll.contentOffset = CGPointMake(WIDTH, 0);
        }
        
        self.page.currentPage = self.scroll.contentOffset.x / WIDTH;
        
        [UIView animateWithDuration:0.3 animations:^{
            
            int contentOfset = (int)self.scroll.contentOffset.x;
            
            self.scroll.contentOffset = CGPointMake(contentOfset + WIDTH, 0);
        }];
    }
}

//移除定时器
- (void)removeTimer{
    
    [self.timer invalidate];
    self.timer = nil;
}

#pragma mark - 当用户开始退拽的时候调用移除计时器方法
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self removeTimer];
    
}

#pragma mark -- 当用户停止拖拽时, 调用定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self setUpTimer];
    
}

- (void)awakeFromNib {
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
}


@end
